#!/bin/bash
###############################################################################
# This program is used to insert manual recommendation automatically
# FOLLOW BY: 
#   1.truncate the table manualseries manually
#   2.run this shell script
#   3.restart the Tomcat server.
# LOGIC:
#   1. fetch the hottest series from "hotseriesbytype" table
#   2. use curl command to get the natual recommendation of each of above series
#   3. extract the returned seriesids
#   4. insert the desired program into "manualseries" table
#   5. insert the returned seriesids into "manualseries" table
# VALIDAT: use the following 2 SQL to valid result
#   1. select s.name, p.name, h.viewtimes from manualseries m, hotseriesbytype h, series s, program p where m.leftseries=s.seriesid and m.rightseries=p.programid and h.seriesid=m.leftseries order by h.viewtimes desc;
#   2. select distinct p.name from manualseries m, program p where m.rightseries=p.programid;
###############################################################################
master_host=10.12.190.9
portal_host=10.12.190.4


if [ $# -eq 0 ];then
    echo 'missing the porgram id...'
    exit 0
fi

existing=`mysql -uroot -pmysql -h$master_host -e"select distinct p.programid from recommendationengine.manualseries m, recommendationengine.program p where m.rightseries=p.programid" | grep -v programid`

echo 'truncate table...'
mysql -uroot -pmysql -h$master_host -e "truncate recommendationengine.manualseries;"

batch=0
for pid in $*  # for all input parameters
do
echo $pid
    echo 'get top 20 series...'
    hotseries=`mysql -uroot -pmysql -h$master_host -e "select seriesid from recommendationengine.hotseriesbytype where type=1 order by viewtimes desc limit 20"  | grep -v seriesid | awk NR%$#==$batch`
    echo $hotseries
    for hsid in $hotseries
    do
        xml=`curl "http://$portal_host:8080/RecommendationService_1.0.1/recommend?method=seriesbasedrecommend&sid=$hsid&bygenre=true"`
        #echo $xml
        series=`echo $xml | awk -F'<iid>' '{print substr($2,0,index($2,"<")-1)"\n"substr($3,0,index($3,"<")-1)"\n"substr($4,0,index($4,"<")-1)"\n"substr($5,0,index($5,"<")-1)"\n"substr($6,0,index($6,"<")-1)"\n"substr($7,0,index($7,"<")-1)"\n"substr($8,0,index($8,"<")-1)"\n"substr($9,0,index($9,"<")-1)"\n"substr($10,0,index($10,"<")-1)"\n"substr($11,0,index($11,"<")-1)"\n"}'`
        #echo $series
        mysql -uroot -pmysql -h$master_host -e "insert into recommendationengine.manualseries values('$hsid', '$pid', 1)"
        i=2
        for rsid in $series
        do
            echo $i
            if [ $(echo $existing | grep $rsid | wc -l) -eq 0 ] # use this judegment to avoid existing memcached manual recommendation to enter again
            then
                mysql -uroot -pmysql -h$master_host -e "insert into recommendationengine.manualseries values('$hsid', '$rsid', $i)"
                i=$((i+1))
            fi
        done
    done
    batch=$((batch+1))
done

echo 'done!'
echo ''
echo 'NOTICE: The tomcat on the portal MUST be restarted.'

# select s.name, p.name, h.viewtimes as hotness from series s, manualseries ms, program p, hotseriesbytype h where s.seriesid=ms.leftseries and ms.rightseries=p.programid and s.seriesid=h.seriesid order by h.viewtimes desc;

